package com.tuxianchao.demo.springbootasync.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * created by
 *
 * @author tuxianchao
 * @date 2018/6/3 下午4:29
 */
@Component
public class Task {

    @Async
    public Future<String> taskOne() throws InterruptedException {
        System.out.println("任务一开始");
        System.out.println(Thread.currentThread().getName());
        long start = System.currentTimeMillis();
        Thread.sleep(1000);
        System.out.println("任务一结束，耗时" +
                (System.currentTimeMillis() - start) + "ms");
        return new AsyncResult<>("任务一完成");


    }

    @Async
    public Future<String> taskTwo() throws InterruptedException {
        System.out.println("任务二开始");
        System.out.println(Thread.currentThread().getName());
        long start = System.currentTimeMillis();
        Thread.sleep(1000);
        System.out.println("任务二结束，耗时" +
                (System.currentTimeMillis() - start) + "ms");
        return new AsyncResult<>("任务二完成");

    }

    @Async
    public Future<String> taskThree() throws InterruptedException {
        System.out.println("任务三开始");
        System.out.println(Thread.currentThread().getName());
        long start = System.currentTimeMillis();
        Thread.sleep(1000);
        System.out.println("任务三结束，耗时" +
                (System.currentTimeMillis() - start) + "ms");
        return new AsyncResult<>("任务三完成");
    }
}
